草庐IT

带指针的 C++ vector

全部标签

vector 中不存在 C++ 字符串成员变量

我正在创建一个vector,其中包含指向基类的指针。在此vector中,我动态存储指向包含一些成员变量的派生类的指针,其中之一是字符串变量名。#include"stdafx.h"#include#include#include#includeboolhasDirection=false;boolhasDiameter=false;intdirection;floatdiameter;intstarDimension=0;intanimalDimension=0;intfishDimension=0;classMovingObject{protected:std::stringname;

c++ - qt垃圾回收和智能指针

我正在考虑在我的qt工作中开始使用智能指针。让我感到困惑的是智能指针如何与Qt垃圾收集一起使用。整个Qt依赖于子QObject以QObject*parent作为ctor参数构造的习语,因此启用垃圾收集。例如:QWidget*mWidget=newQWidget(this);//Herewenotonly//ensurethatmWidgetwillbedeleted//whenitsparentisdeleted,butalsotellqt,//thatmWidgetisnotawindow,butbelongsto//parent'slayout现在,如果我想将mWidget包装到智

c++ - 变量 vector 与指针 vector

我只是好奇使用变量vector与使用动态内存指针vector的区别,我发现了一些让我困惑的事情。我有一个简单的main.cpp,看起来像这样,#include#includeusingnamespacestd;classA{public:A(){x=2;}virtual~A(){coutlist;list.push_back(B());list.push_back(A());list.push_back(B());list.push_back(C());list.push_back(A());for(std::vector::iteratorit=list.begin();it!=li

c++ - 指针被释放

我不明白下面的代码有什么问题。它会生成“正在释放的指针未分配”错误。#include"mpi.h"usingnamespacestd;voidchangeArray(bool*isPrime){delete[]isPrime;isPrime=newbool[10];}intmain(intargc,char*argv[]){intsize,rank;MPI_Init(&argc,&argv);MPI_Comm_size(MPI_COMM_WORLD,&size);MPI_Comm_rank(MPI_COMM_WORLD,&rank);bool*isPrime=newbool[1000]

c++ - vector::clear() 幕后和破坏

我研究过STLvector的实现。vector容器被实现为一个动态数组。方法clear()用于破坏vector中的所有元素,它将vector的大小设置为0,但容量保持不变。所以,如果我理解正确的话,所有的元素都被称为它们的析构函数,但是动态分配的内存仍然可用。为了仍然释放它,我们可以这样做:Vec.swap(vector());//Capacity=0.但是假设我们没有使用swap,只是做了一个clear。内部实现(如果我错了请纠正我)大约等于以下内容(以非常简化的方式)://Acontainedtype:structC{intm;C():m(123){}};C*arr=newC[10

c++ - 按值传递,常量值,引用,常量引用,指针,常量指针

探索更多并找到答案以确定如何传入oldpost(抱歉重复)如果函数打算改变参数作为副作用,取它通过非常量引用。如果函数不修改它的参数且参数为原始类型,按值取值。否则按const引用取,以下情况除外如果函数需要复制const引用不管怎样,按值(value)来衡量。[原帖在下方]我想总结一下按值传递、const值、引用、const引用、指针、const指针的使用,请大家指正和建议。对于引用和指针,尽可能使用const(感谢大家)。通过引用和指针传递之间没有性能差异。当大小不大于指针时(感谢MarkRansom),按值传递。还有一些问题:我很少看到传递const值。它是否有用,或者编译器会检

c++ - GCC 4.7.2:带有指向成员函数指针的 std::thread

正在为thisquestion写测试代码我发现下面的注释行无法在GCC4.7.2上编译:#include#includestructS{voidf(){std::cout但cppreference似乎声称“this”参数可以等效地作为对象、对象引用或对象指针传递:IffispointertoamemberfunctionofclassT,thenitiscalled.Thereturnvalueisignored.Effectively,thefollowingcodeisexecuted:(t1.*f)(t2,...,tN)ifthetypeoft1iseitherT,referen

c++ - 如何在 Eigen3 中调整 Vector 的大小

我将两个Eigen3vector包装在一个模板化的LineSegment中类(class)。你可以像这样使用它:typedefLineSegmentLineSegment2d;typedefLineSegmentLineSegment3d;typedefLineSegmentLineSegment3i;它包含一个模板化的方法来改变组件的尺寸。这是修剪后的定义:templatestructLineSegment{public:templateLineSegmentto()const{Eigen::Matrixnewp1;Eigen::Matrixnewp2;//TODOinitialis

c++ - 转换 std::vector 为 boost::variant 类型

我如何实现下面的函数来从Value的vector进行转换?到Container?如果不是values的所有成员,我想断言是同一类型,即如果vector包含字符串和整数的混合。这是因为函数的返回值是std::vector。或std::vector.typedefboost::variantValue;typedefboost::variant,std::vector>Container;ContainervaluesToContainer(conststd::vector&values){returnContainer();} 最佳答案

c++ - 在智能指针上调用成员函数指针

在处理类成员函数指针时,我们可以使用以下语法在对象实例上调用函数:structX{voidfoo();};Xx;//Instanceautof=&X::foo;//Memberfunctionpointer(x.*f)();//Callfunctionfonx当有一个实例的原始指针时,语法是这样的:X*xptr=newX();(xptr->*f)();这很好地遵循了静态函数调用的类比:x.foo();x->foo();但是,当我有类的智能指针时,这不起作用:unique_ptrxsptr(newX());(xsptr->*f)();//g++:error:nomatchfor'oper